/*
 * contrib/btree_gist/btree_cash.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "common/int.h"
#include "utils/cash.h"

typedef struct
{
	Cash		lower;
	Cash		upper;
} cashKEY;

/*
** 现金操作
*/
PG_FUNCTION_INFO_V1(gbt_cash_compress);
PG_FUNCTION_INFO_V1(gbt_cash_fetch);
PG_FUNCTION_INFO_V1(gbt_cash_union);
PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
PG_FUNCTION_INFO_V1(gbt_cash_consistent);
PG_FUNCTION_INFO_V1(gbt_cash_distance);
PG_FUNCTION_INFO_V1(gbt_cash_penalty);
PG_FUNCTION_INFO_V1(gbt_cash_same);

static bool fc_gbt_cashgt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const Cash *) fc_a) > *((const Cash *) fc_b));
}
static bool fc_gbt_cashge(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const Cash *) fc_a) >= *((const Cash *) fc_b));
}
static bool fc_gbt_casheq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const Cash *) fc_a) == *((const Cash *) fc_b));
}
static bool fc_gbt_cashle(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const Cash *) fc_a) <= *((const Cash *) fc_b));
}
static bool fc_gbt_cashlt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const Cash *) fc_a) < *((const Cash *) fc_b));
}

static int fc_gbt_cashkey_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	cashKEY    *fc_ia = (cashKEY *) (((const Nsrt *) fc_a)->t);
	cashKEY    *fc_ib = (cashKEY *) (((const Nsrt *) fc_b)->t);

	if (fc_ia->lower == fc_ib->lower)
	{
		if (fc_ia->upper == fc_ib->upper)
			return 0;

		return (fc_ia->upper > fc_ib->upper) ? 1 : -1;
	}

	return (fc_ia->lower > fc_ib->lower) ? 1 : -1;
}

static float8 fc_gbt_cash_dist(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return GET_FLOAT_DISTANCE(Cash, fc_a, fc_b);
}


static const gbtree_ninfo tinfo =
{
	gbt_t_cash,
	sizeof(Cash),
	16,							/* sizeof(gbtreekey16) */
	fc_gbt_cashgt,
	fc_gbt_cashge,
	fc_gbt_casheq,
	fc_gbt_cashle,
	fc_gbt_cashlt,
	fc_gbt_cashkey_cmp,
	fc_gbt_cash_dist
};


PG_FUNCTION_INFO_V1(cash_dist);
Datum cash_dist(PG_FUNCTION_ARGS)
{
	Cash		fc_a = PG_GETARG_CASH(0);
	Cash		fc_b = PG_GETARG_CASH(1);
	Cash		fc_r;
	Cash		fc_ra;

	if (pg_sub_s64_overflow(fc_a, fc_b, &fc_r) ||
		fc_r == PG_INT64_MIN)
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("money out of range")));

	fc_ra = Abs(fc_r);

	PG_RETURN_CASH(fc_ra);
}

/**************************************************
 * 现金操作
 **************************************************/


Datum gbt_cash_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_compress(fc_entry, &tinfo));
}

Datum gbt_cash_fetch(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_fetch(fc_entry, &tinfo));
}

Datum gbt_cash_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	Cash		fc_query = PG_GETARG_CASH(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	cashKEY    *fc_kkk = (cashKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_query, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo,
									  fcinfo->flinfo));
}


Datum gbt_cash_distance(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	Cash		fc_query = PG_GETARG_CASH(1);

	/* Oid		subtype = PG_GETARG_OID(3); */
	cashKEY    *fc_kkk = (cashKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_FLOAT8(gbt_num_distance(&fc_key, (void *) &fc_query, GIST_LEAF(fc_entry),
									  &tinfo, fcinfo->flinfo));
}


Datum gbt_cash_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc(sizeof(cashKEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_cash_penalty(PG_FUNCTION_ARGS)
{
	cashKEY    *fc_origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	cashKEY    *fc_newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);

	penalty_num(fc_result, fc_origentry->lower, fc_origentry->upper, fc_newentry->lower, fc_newentry->upper);

	PG_RETURN_POINTER(fc_result);
}

Datum gbt_cash_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_cash_same(PG_FUNCTION_ARGS)
{
	cashKEY    *fc_b1 = (cashKEY *) PG_GETARG_POINTER(0);
	cashKEY    *fc_b2 = (cashKEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
